from aiogram_dialog import Dialog, Window
from aiogram_dialog.widgets.kbd import Back, Button, Cancel, ScrollingGroup, Select
from aiogram_dialog.widgets.text import Const, Format

from src.presentation.tg_bot.dialogs.getters.orders import all_orders_getter
from src.presentation.tg_bot.dialogs.getters.proxies import available_proxies_for_replace_getter, order_proxies_getter
from src.presentation.tg_bot.dialogs.widgets import LocaleText
from src.presentation.tg_bot.states import ChangeProxySG

from .handlers import confirm_changes, set_new_proxy_id, set_order_id, set_proxy_id

select_order_window = Window(
    Const("Выберите заказ"),
    ScrollingGroup(
        Select(
            Format("Order №{item.id} {item.summ}$"),
            "ordersel",
            lambda order: order.id,
            "orders",
            on_click=set_order_id,
        ),
        width=2,
        height=4,
        id="orderselect",
        hide_on_single_page=True,
    ),
    Cancel(LocaleText("back-btn")),
    state=ChangeProxySG.orders_list,
    getter=all_orders_getter,
)

select_proxy_window = Window(
    Const("Какой прокси заменить?"),
    ScrollingGroup(
        Select(
            Format("{item.login}:{item.password}:{item.port_https}"),
            "proxiessel",
            lambda proxy: proxy.id,
            "proxies",
            on_click=set_proxy_id,
        ),
        width=2,
        height=4,
        id="proxysel",
        hide_on_single_page=True,
    ),
    Back(LocaleText("back-btn")),
    state=ChangeProxySG.proxies_list,
    getter=order_proxies_getter,
)

select_new_sproxy_window = Window(
    Const("Выберите новый прокси"),
    ScrollingGroup(
        Select(
            Format("Proxy №{item.id} {item.login}:{item.password}:{item.port_https}"),
            "proxiessel",
            lambda proxy: proxy.id,
            "available_proxies",
            on_click=set_new_proxy_id,
        ),
        width=1,
        height=5,
        id="proxysel",
        hide_on_single_page=True,
    ),
    Back(LocaleText("back-btn")),
    state=ChangeProxySG.select_proxy,
    getter=available_proxies_for_replace_getter,
)

confirm_window = Window(
    Const("Подтвердить изменение?"),
    Button(Const("Да"), id="yes", on_click=confirm_changes),
    Button(Const("Нет"), id="no", on_click=confirm_changes),
    Back(LocaleText("back-btn")),
    state=ChangeProxySG.confirm,
)

change_proxy_dialog = Dialog(
    select_order_window,
    select_proxy_window,
    select_new_sproxy_window,
    confirm_window,
)
